class SqlHandle {

    insertRowCovert(tableName, columns, rows) {
        let temp = [];
        temp.push(`insert INTO ${tableName} (`);
        for (const c of columns) {
            temp.push(c.field)
            temp.push(',')
        }
        temp = temp.slice(0, -1)
        temp.push(`) VALUES  `);
        let value = "";
        for (const row of rows) {
            temp.push(' (')
            for (const c of columns) {
                value = this.covertValue(row[c.field],c)
                if(value===''){
                    value = 'null'
                }
                temp.push(value)
                temp.push(',')
            }
            temp = temp.slice(0, -1)
            temp.push(' )')
            temp.push(' ,')
        }
        temp = temp.slice(0, -1)
        temp.push(`;`);
        return temp.join("");

    }
    updateRowCovert(tableName, columns, rows) {
        const arrs = [];
        let temp = [];
        let value = "";
        const headContent = `UPDATE ${tableName} `
            for (const row of rows) {
                temp.push(headContent);
                temp.push(' SET ')
               const  primaryKeyValues =  row.primaryKeyValues;
                for (const c of columns) {
                    value = this.covertValue(row[c.field],c)
                    if(value===''){
                        value = 'null'
                    }
                    temp.push(` ${c.field}=${value}`)
                    temp.push(',')
                }
                temp = temp.slice(0, -1)
                temp.push(" WHERE ")
                columns.filter(c => c.primaryKey).forEach(element => {
                   const value =   primaryKeyValues[element.field];
                   if(value){
                    temp.push(`${element.field}=${this.covertValue(value,element)}`)
                   }else{
                    temp.push(`${element.field} is NULL`)
                   }
                   temp.push(' AND ');           
                });
                temp = temp.slice(0, -1)
                temp.push(`;`);
                arrs.push(temp.join(""));
            }
            return arrs;

    }
    deleteRowCovert(tableName, columns, rows) {
        const arrs = [];
        let temp = [];
        const headContent = `DELETE FROM  ${tableName} `
            for (const row of rows) {
                temp.push(headContent);
               const  primaryKeyValues =  row.primaryKeyValues;
                temp.push(" WHERE ")
                columns.filter(c => c.primaryKey).forEach(element => {
                   const value =   primaryKeyValues[element.field];
                   if(value){
                    temp.push(`${element.field}=${this.covertValue(value,element)}`)
                   }else{
                    temp.push(`${element.field} is NULL`)
                   }
                   temp.push(' AND ');           
                });
                temp = temp.slice(0, -1)
                temp.push(`;`);
                arrs.push(temp.join(""));
            }
            return arrs;
    }

    covertValue(value,column){
        if(value==null||value==undefined){
            return ''
        }
        if(column.type==='NUMBER'){
            return value;
        }
        return  `'${value}'`
        

    }

}

export {
    SqlHandle
}